package iscript.uiFramework.components.silder
{
	import flash.display.Bitmap;
	import flash.events.MouseEvent;
	import flash.utils.setInterval;
	import iscript.uiFramework.components.button.ImageButton;
	import iscript.uiFramework.event.SilderEvent;
	import iscript.uiFramework.UIFramework;
	import iscript.uiFramework.util.Scale9GridBitmap;
	
	public class SilderBar extends AbstractSilder
	{
		
		private var _minWidth:int = 50;
		private var _IsDown:Boolean;
		private var _IsRollOver:Boolean;
		private var _blockScale9Grid:Scale9GridBitmap;
		//private var _buttonControl:Boolean;
		
		
		public function SilderBar()
		{
			_inner = true;
			
			super();
		}
		
		override protected function initialize():void
		{
			_blockScale9Grid = new Scale9GridBitmap(UIFramework.resource.getBitmapData("GameLite.UIComponent."+this.theme+".SilderBar.Block.Normal"), 
													UIFramework.resource.getElementRectangle("SilderBar", this.theme));
													
			silderDesinger = new AbstractSilderDesinger(this.theme, _inner, UIFramework.resource.getElementRectangle("SilderBar", this.theme));
			silderDesinger.clingBitmap = UIFramework.resource.getBitmapData("GameLite.UIComponent."+this.theme+".SilderBar.Background");
			silderDesinger.topButton = new ImageButton(UIFramework.resource.getBitmapData("GameLite.UIComponent."+this.theme+".SilderBar.UpButton.Normal"), 
													   UIFramework.resource.getBitmapData("GameLite.UIComponent."+this.theme+".SilderBar.UpButton.Hover"))
			
			silderDesinger.bottomButton = new ImageButton(UIFramework.resource.getBitmapData("GameLite.UIComponent."+this.theme+".SilderBar.BottomButton.Normal"), 
														  UIFramework.resource.getBitmapData("GameLite.UIComponent."+this.theme+".SilderBar.BottomButton.Hover"))
			
		    silderDesinger.topButton.HoldDownHandler = _UPButtonLogic
			silderDesinger.topButton.ClickHandler = _UPButtonLogic;
			silderDesinger.bottomButton.ClickHandler = _DOWNButtonLogic;
			silderDesinger.bottomButton.HoldDownHandler = _DOWNButtonLogic
			silderDesinger.bottomBitmap = UIFramework.resource.getBitmapData("GameLite.UIComponent."+this.theme+".SilderBar.Background");
			silderDesinger.setBlock(_blockScale9Grid);
			addChild(silderDesinger);
			
			super.initialize();
			
			silderDesinger.block.addEventListener(MouseEvent.ROLL_OVER, _blockRollOverLogic)
			silderDesinger.block.addEventListener(MouseEvent.ROLL_OUT, _blockRollOutLogic)
			UIFramework.UiStage.addEventListener(MouseEvent.MOUSE_UP, _blockMouseUpLogic)
			UIFramework.UiStage.addEventListener(MouseEvent.MOUSE_DOWN, _blockMouseDownLogic)
		
		}
		
		
		
		
		
		private function _blockRollOutLogic(e:MouseEvent):void
		{
			if (_IsRollOver && !_IsDown)
			{
				_blockScale9Grid.updateState(UIFramework.resource.getBitmapData("GameLite.UIComponent."+this.theme+".SilderBar.Block.Normal"));
				_IsRollOver = false;
			}
		}
		
		private function _blockMouseDownLogic(e:MouseEvent):void
		{
			if (_IsRollOver)
			{
				_blockScale9Grid.updateState(UIFramework.resource.getBitmapData("GameLite.UIComponent."+this.theme+".SilderBar.Block.Down"));
				_IsDown = true;
			}
		}
		
		private function _blockMouseUpLogic(e:MouseEvent):void
		{
			if (_IsRollOver)
			{
				_blockScale9Grid.updateState(UIFramework.resource.getBitmapData("GameLite.UIComponent."+this.theme+".SilderBar.Block.Normal"));
				_IsDown = false;
				_IsRollOver = false;
			}
		}
		
		private function _blockRollOverLogic(e:MouseEvent):void
		{
			if (!_IsDown)
			{
				_blockScale9Grid.updateState(UIFramework.resource.getBitmapData("GameLite.UIComponent."+this.theme+".SilderBar.Block.Hover"));
				_IsRollOver = true;
			}
		}
		
		override public function set width(value:Number):void
		{
			value = value < _minWidth ? _minWidth : value;
			super.width = value;
		}
		
/*		public function get buttonControl():Boolean 
		{
			return _buttonControl;
		}
		
		public function set buttonControl(value:Boolean):void 
		{
			_buttonControl = value;
			silderDesinger.topButton.visible = value;
			silderDesinger.bottomButton.visible = value;
		}*/
		
		/**
		 * 向上箭头按钮
		 * @param	e
		 */
		private function _UPButtonLogic(e:*):void
		{
			currentValue -= 3
			dispatchSilderEvent();
		}
		/**
		 * 向下箭头按钮
		 * @param	e
		 */
		private function _DOWNButtonLogic(e:*):void
		{
			currentValue += 3;
			dispatchSilderEvent();
		}
		

		override public function dispose():void
		{
			if (!Disposed)
			{
				silderDesinger.block.removeEventListener(MouseEvent.ROLL_OVER, _blockRollOverLogic)
				UIFramework.UiStage.removeEventListener(MouseEvent.MOUSE_UP, _blockMouseUpLogic)
				UIFramework.UiStage.removeEventListener(MouseEvent.MOUSE_DOWN, _blockMouseDownLogic)
				super.dispose()
			} 
		}
	}

}